package cn.explink.dop.controller;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.explink.dop.entity.vo.DeliveryManVO;
import cn.explink.dop.entity.vo.OrderVO;
import cn.explink.dop.entity.vo.ReturnVO;
import cn.explink.dop.service.DeliveryManService;
import cn.explink.dop.service.OrderService;
import cn.explink.dop.service.ReturnService;
import cn.explink.eap.web.cgform.controller.autolist.CgAutoListController;

/**
 *
 * @author wangzy
 */
@Controller
@RequestMapping("/returnCountController")
public class ReturnCountController extends CgAutoListController {

	@Autowired
	private OrderService orderService;

	@Autowired
	private ReturnService returnService;

	@Autowired
	private DeliveryManService deliveryManService;

	/**
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "index1")
	public ModelAndView storefrontIndex(HttpServletRequest request) {
		ModelAndView view = new ModelAndView("dop/settlement/returnCount");
		List<DeliveryManVO> deliveryManList = this.deliveryManService.getListData(new DeliveryManVO());
		view.addObject("manList", deliveryManList);
		return view;
	}

	/**
	 * 获取数据
	 *
	 * @param pageNum
	 *            当前页
	 * @param pageSize
	 *            当前页条数
	 * @param request
	 *            请求
	 * @return 结果集
	 */
	@RequestMapping(params = "loadData")
	@ResponseBody
	@SuppressWarnings("unchecked")
	public List<OrderVO> loadData(int pageNum, int pageSize, HttpServletRequest request) {
		Map<String, Object> map = this.orderService.getListDataByPage(new OrderVO(), null, pageNum, pageSize);
		return (List<OrderVO>) map.get("list");
	}

	/**
	 *
	 * @param orderVO
	 * @param pageNum
	 * @param pageSize
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "query1")
	@ResponseBody
	public Map<String, Object> queryData11(ReturnVO returnVO, int pageNum, int pageSize, HttpServletRequest request) {
		// return this.orderService.getListDataByPage(orderVO, pageNum,
		// pageSize);
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			String person = request.getParameter("deliveryMan");
			String startdeliveryTime = request.getParameter("startdeliveryTime");
			Map<String, Object> mapReturn = this.returnService.queryReturn(returnVO, pageNum, pageSize);
			// String enddeliveryTime = request.getParameter("enddeliveryTime");
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

			int countN = 15;
			int count = 0;
			List<ReturnVO> list = new ArrayList<ReturnVO>();
			List<ReturnVO> listTotal = new ArrayList<ReturnVO>();
			ReturnVO totalvo = new ReturnVO();

			int num = 0;
			int money = 0;
			BigDecimal salary = new BigDecimal(0);
			totalvo.setSendPersonName("汇总：");
			listTotal.add(totalvo);
			for (int i = 0; i < 10; i++) {
				ReturnVO vo = new ReturnVO();
				vo.setNumber(countN++);
				vo.setMoney(Float.parseFloat("1000" + (count += 100)));
				vo.setSendPersonName(person);
				vo.setCreateTime(new Date());
				if (startdeliveryTime != "") {
					vo.setCreateTime(format.parse(startdeliveryTime));
				}
				// if(enddeliveryTime != ""){
				// vo.setEndTime(format.parse(enddeliveryTime));
				// }
				vo.setStatus("已交款");
				vo.setSalary(new BigDecimal(200d + (countN += 20d)));
				list.add(vo);
				num += vo.getNumber();
				salary = vo.getSalary().add(vo.getSalary());
				money += 1000 + count;
			}
			totalvo.setNumber(num);
			totalvo.setSalary(salary);
			totalvo.setMoney(Float.parseFloat("19005500"));

			// map.put("count", 10);
			map.put("list", list);
			// map.put("footer", listTotal);

			return map;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 *
	 * @param orderVO
	 * @param pageNum
	 * @param pageSize
	 * @param request
	 * @return
	 * @throws ParseException
	 */
	@RequestMapping(params = "query")
	@ResponseBody
	public Map<String, Object> queryData(ReturnVO returnVO, int pageNum, int pageSize, HttpServletRequest request) throws ParseException {
		Map<String, Object> map = new HashMap<String, Object>();
		String person = request.getParameter("deliveryMan");
		person = person.replace("[", "(");
		person = person.replace("]", ")");
		String startdeliveryTime = request.getParameter("startdeliveryTime");
		String enddeliveryTime = request.getParameter("enddeliveryTime");
		returnVO.setSendPersonName(person);
		SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-dd");
		Date startDate = null;
		Date endDate = null;
		if ("" != startdeliveryTime) {
			startDate = sp.parse(startdeliveryTime);
		}
		if ("" != enddeliveryTime) {
			endDate = sp.parse(enddeliveryTime);
		}
		returnVO.setCreateTime(startDate);
		returnVO.setEndTime(endDate);
		Map<String, Object> mapReturn = this.returnService.queryReturn(returnVO, pageNum, pageSize);
		return mapReturn;
	}

	/**
	 *
	 * @param status
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "getOrderListByStatus")
	@ResponseBody
	public Map<String, Object> getOrderListByStatus(@RequestParam("statusList[]") List<Integer> statusList, int pageNum, int pageSize, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if ((null == statusList) || (statusList.size() == 0)) {
			return resultMap;
		}

		// TODO 分页 不过3秒刷新一次，估计分页用处不大
		Integer count = this.orderService.getCountByStatus(null, statusList);
		List<OrderVO> orderVOList = this.orderService.getOrderVOListByStatusAndDate(null, statusList, null, null);

		resultMap.put("count", count);
		resultMap.put("list", orderVOList);
		return resultMap;
	}
}
